<template>
  <div class="goodsDetail">
    <!-- 上部分内容区 -->
    <el-row :gutter="12">
      <el-col :span="8">
        <div class="HeadLOGO">
          <img :src="goodsInfo.coverUrl || emptyImg"/>
        </div>
      </el-col>

      <el-col :span="16" style="display: flex">
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-card shadow="hover" style="margin-left: 0px">
              <span style="font-size: 24px">{{ goodsInfo.goodsName }}</span>
              <span price style="font-size: 18px"
              >商品价格: ￥{{ goodsInfo.price }}</span
              >
              <div style="height: 25px"></div>
              <span browserInfo style="font-size: 14px"
              >{{ goodsInfo.wantCount }}人想要</span
              >
              <div style="height: 32px"></div>
            </el-card>
            <el-button
                type="primary"
                @click="dialogWantVisible = true"
                :disabled="!token"
                icon="el-icon-search"
            >我想要
            </el-button
            >
          </div>
        </el-col>
        <el-col :span="16">
          <span class="publish">发布者：{{ goodsInfo.publisherName }}</span>
          <div style="height: 20px"></div>
          <GaoDeMap v-if="goodsInfo.goodsLng && goodsInfo.goodsLat"
                    :markers="[[goodsInfo.goodsLng, goodsInfo.goodsLat]]"
                    :center="[goodsInfo.goodsLng, goodsInfo.goodsLat]"
          />
        </el-col>
      </el-col>
    </el-row>
    <div style="height: 10px"></div>

    <h2 style="float: left; margin-bottom: 6px">商品详情:</h2>
    <el-divider content-position="left"></el-divider>
    <!-- 商品详情 -->
    <div style="height: 10px"></div>
    <div class="details" style="white-space: normal">
      {{ goodsInfo.goodsDes }}
    </div>
    <h2 style="float: left; margin-bottom: 6px">商品评价:</h2>
    <el-button
        type="primary"
        @click="dialogFormVisible = true"
        icon="el-icon-search"
        :disabled="!token"
        style="float: right; margin-top: -20px"
    >我想评论
    </el-button
    >
    <el-divider content-position="left"></el-divider>
    <!-- 商品评价 -->
    <div class="comments-list">
      <li class="List" v-for="item in comments" :key="item.id">
        <el-card shadow="hover">
          <div class="head">
            <img src="@/assets/logo.png" alt="" style="vertical-align: left"/>
            <a style="margin-right: 30px">{{ item.replyerName }}</a>
            <a style="margin-right: 0px"
            >发布于: {{ item.createdTime | parseTime }}</a
            >
          </div>
          <div class="comment">{{ item.comments }}</div>
          <div style="height: 20px"></div>
        </el-card>
      </li>
    </div>
    <!-- 想要 -->

    <el-dialog title="我想要" :visible.sync="dialogWantVisible">
      <el-form
          ref="dataForm"
          :model="temp"
          label-position="left"
          label-width="120px"
          style="width: 500px; margin-left: 50px"
      >
        <el-form-item label="买家留言">
          <el-input v-model="temp.des" type="textarea" :rows="6"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogWantVisible = false"> 取消</el-button>
        <el-button type="primary" @click="want"> 确定</el-button>
      </div>
    </el-dialog>
    <!-- 评论 -->
    <el-dialog title="添加评论" :visible.sync="dialogFormVisible">
      <el-form
          ref="dataForm"
          :rules="rules"
          :model="temp"
          label-position="left"
          label-width="120px"
          style="width: 500px; margin-left: 50px"
      >
        <el-form-item label="买家留言" prop="comments">
          <el-input type="textarea" :rows="6" v-model="temp.comments"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消</el-button>
        <el-button type="primary" @click="createComment"> 确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import {parseTime} from "@/utils";
import {goodsRestful} from "@/api/goods";
import {commentsRestful} from "@/api/comment";
import {goodsTradingRestful} from "@/api/goods-trading";
import GaoDeMap from "@/components/GaoDeMap";

export default {
  data() {
    return {
      emptyImg: require('@/assets/empty.jpg'),
      goodsInfo: {},
      comments: [],
      dialogFormVisible: false,
      dialogWantVisible: false,
      rules: {
        comments: [
          {required: true, message: "评论内容不能为空", trigger: "change"},
        ],
      },
      temp: {
        comments: "",
        des: "",
      },
    };
  },
  methods: {
    fetchGoods() {
      goodsRestful.fetchAll({id: this.$route.query.id}).then((data) => {
        this.goodsInfo = this._.get(data, "data.0");
      });
    },
    fetchComments() {
      commentsRestful
          .fetchAll({goodsId: this.$route.query.id})
          .then((data) => {
            this.comments = data.data;
          });
    },
    want() {
      goodsTradingRestful.createData({goodsId: this.$route.query.id, ...this.temp}).then((data) => {
        this.$message.success(data.message);
        this.dialogWantVisible = false;
      });
    },
    createComment() {
      commentsRestful
          .createData({goodsId: this.$route.query.id, ...this.temp})
          .then((data) => {
            if (data.status === "00000") {
              this.dialogFormVisible = false;
              this.fetchComments();
            }
          });
    },
  },
  created() {
    this.fetchGoods();
    this.fetchComments();
  },
  filters: {
    parseTime,
  },
  components: {
    GaoDeMap,
  },
  computed: {
    ...mapGetters(['token']),
  }
};
</script>

<style lang="less" scoped>
.goodsDetail {
  padding: 24px 10% 24px 10%;

  .el-row {
    .el-col:nth-child(2) {
      margin-top: 12px;
    }
  }

  .HeadLOGO {
    padding-top: 12px;

    img {
      width: 320px;
    }
  }
}

.main {
  height: auto;
  margin-bottom: 20px;
  height: 1000px;
  overflow: hidden;

  &:last-child {
    margin-bottom: 0;
  }

  .el-col {
    border-radius: 4px;
    height: 200px;
  }

  .row-bg {
    padding: 10px 0;
    background-color: #e9eef3;
  }
}

.details {
  margin-left: 150px;
  margin-right: 100px;
  line-height: 30px;
}

.el-divider__text {
  position: absolute;
  background-color: #fff;
  padding: 0 20px;
  font-weight: 500;
  color: #303133;
  font-size: 25px;
}

.el-divider--horizontal {
  height: 2px;
  margin-top: 32px;
}

.grid-content bg-purple {
  text-align: left;
  background-color: #000000;
}

.el-card is-hover-shadow {
  text-align: left;
}

.el-col el-col-4 {
  margin-right: 60px;
}

.publishAndMap {
  width: 100%;
  margin-top: 30px;
  margin-right: 10px;
}

.userLogo {
  width: 15px;
  height: 15px;
  text-align: center;
  margin-top: 0px;
  margin-bottom: 0px;
}

.Map {
  width: 80px;
  height: 80px;
  text-align: left;
  margin-left: 10px;
  margin-bottom: 0px;
}

::marker {
  color: rgb(255, 255, 255);
  font-size: 1.2em;
}

.el-card {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #ffffff;
  border: #fff;
  text-align: -moz-left;

  span {
    display: block;
    // font-weight: bold;
    // font-weight: 400;
    line-height: 100%;
    margin-top: 10px;
  }

  span[price] {
    font-size: 12px;
  }

  span[browserInfo] {
    font-size: 12px;
  }

  .head {
    width: 800px;
    height: 50px;
    text-align: left;
    margin-left: 150px;
    margin-bottom: 0px;

    img {
      width: 30px;
      height: 30px;
      text-align: center;
      margin-top: 10px;
      margin-bottom: 0px;
    }
  }

  .time {
    margin-right: 10px;
  }

  .comment {
    text-align: left;
    margin-left: 150px;
  }
}
</style>
